package com.jt.common.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;

public class MD5 {
	
	public final static String KEY = "gofun_2016!@#";

	public final static String getMD5(String s) {
		HashCode hashCode= Hashing.md5().hashString(s, Charsets.UTF_8);  
		return hashCode.toString(); 
//		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
//				'a', 'b', 'c', 'd', 'e', 'f' };
//		try {
//			byte[] strTemp = s.getBytes();
//			// 使用MD5创建MessageDigest对象
//			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
//			mdTemp.update(strTemp);
//			byte[] md = mdTemp.digest();
//			int j = md.length;
//			char str[] = new char[j * 2];
//			int k = 0;
//			for (int i = 0; i < j; i++) {
//				byte b = md[i];
//				// System.out.println((int)b);
//				// 将没个数(int)b进行双字节加密
//				str[k++] = hexDigits[b >> 4 & 0xf];
//				str[k++] = hexDigits[b & 0xf];
//			}
//			return new String(str);
//		} catch (Exception e) {
//			return null;
//		}
	}

	public static String generateToken(String userId, long time) {
		return getMD5(KEY + userId + time);
	}
	
	public static String checkKm(long time) {
		return getMD5(KEY  + time);
	}
	
	/**
	 * 拦截器里的verify验证
	 * @param time
	 * @param verify
	 * @return
	 */
	public static boolean checkVerify(String time,String verify) {
		if(verify.equals(getMD5(KEY  + time))){
			return true;
		}
		return false;
	}
	/**
	 * 验证短信码
	 * @param mobile
	 * @param captchaSign
	 * @return
	 */
	public static boolean checkCaptcha(String mobile,String captchaSign) {
		if(captchaSign!=null){
			if (captchaSign.equals(getMD5(KEY + mobile))) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 验证红包是否有效
	 * @param 
	 * @param 
	 * @return
	 */
	public static boolean checkRedPacket(Integer ordersId,Integer userId,String md) {
		if(md!=null&&ordersId!=null&&userId!=null){
			if (md.equals(getMD5(KEY + ordersId+userId))) {
				return true;
			}
		}
		return false;
	}
	
	public static String compute(String s) {

		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static String genImageMD5URL(String urlString){
		
		String fileName = urlString.substring(urlString.lastIndexOf("/")+1, urlString.length());
		return urlString+"?sign="+getMD5(fileName+KEY);
	}

	/**
	 * 根据文件获取MD5
	 * @param file
	 * @return
	 * @throws FileNotFoundException
     */
	public static String getMd5ByFile(File file) throws FileNotFoundException {
		String value = null;
		FileInputStream in = new FileInputStream(file);
		try {
			MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(byteBuffer);
			BigInteger bi = new BigInteger(1, md5.digest());
			value = bi.toString(16);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return value;
	}

	
	public void getMd5ByFile(){
		File f = new File("E://U4641P1503DT20140120182431.jpg");
		try {
			System.out.println(getMd5ByFile(f));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * md5 32位加密
	 * 
	 * @param s
	 * @return
	 */
	public static String md5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = s.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
